<template>
<div class="app">
  <el-form label-position="left" label-width="80px" :model="user">
    <el-form-item label="账号">
      <el-input v-model="user.userName"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="user.password" show-password></el-input>
    </el-form-item>
    <el-form-item label="昵称">
      <el-input v-model="user.nickName"></el-input>
    </el-form-item>
    <el-form-item label="等级">
      <el-input v-model="user.level"></el-input>
    </el-form-item>
    <el-form-item label="余额">
      <el-input v-model="user.balance"></el-input>
    </el-form-item>
    <div>
      <el-tooltip placement="left">
        <div slot="content">头像上传</div>
        <el-upload
            class="avatar-uploader"
            action="api/User/addProfile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"

            :headers="{
            'Authorization' : this.getToken
          }">
          <img v-if="user.userImg" :src="user.userImg | url" class="avatar" style="width: 100%">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

      </el-tooltip>
    </div>
    <el-button type="primary" @click="onSubmit">提交</el-button>
    <el-button @click="closeDiaLog">取消</el-button>
  </el-form>
</div>
</template>

<script>
import {updateUser} from "@/utils/api";

export default {
  name: "UserEditComponent.vue",
  props: {
    user:Object
  },
  data() {
    return {

    }
  }, methods: {
    closeDiaLog(){

      this.$emit('dialogVisible',false)

    },
    onSubmit(){
      console.log("提交前:")
      console.log(this.user)
      updateUser(this.user).then(res=>{
        if(res.code === 200){
          // console.log(res)
          if(this.getUser.userNumber === this.user.userNumber)
            this.getUser = this.user
          this.notify("更改成功",this.user.userName,"success")
          this.closeDiaLog()
        }else{
          this.notify("更改失败",this.user.userName,"error")
          // console.error(res)
        }
      })

    },
    handleAvatarSuccess(res, file) {
      // console.log(res)
      this.user.userImg = res.objects
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },notify(msg,title,type) {
      this.$notify({
        title: title,
        message: msg,
        type: type
      });
    },
  }, watch: {}, computed: {
    getToken(){
      return localStorage.getItem("token")
    },
    getUser:{
      get(){
        return this.$store.getters.getUser;
      },
      set(value){
        return this.$store.commit("setUser",value);
      }
    },
  },filters:{

    url(value){
      return "api/"+value;
    }
  }
}
</script>

<style scoped>
.app{

}
</style>